package com.example.contextprovider1;

import java.security.acl.Permission;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.example.servicioservidor.IMiRemoto;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

public class ContextProviderTimer extends Service implements ContextProvider {

	private Set<Function<?>> functions;
	private String description;
	private String name;
	private ContextType type;
	private Set<Permission> permissions;
	private Float frecuency;
	private Set<Sensor> sensors;
	IMiRemoto myRemoteService;

	// public ContextProviderTimer(Set<Function<?>> functions, String
	// description,
	// String name, ContextType type, Set<Permission> permissions,
	// Float frecuency, Set<Sensor> sensors) {
	// this.functions = functions;
	// this.description = description;
	// this.name = name;
	// this.type = type;
	// this.permissions = permissions;
	// this.frecuency = frecuency;
	// this.sensors = sensors;
	// }

	public ContextProviderTimer() {
		functions = new HashSet<Function<?>>();
		description = "ContextProvider que provee de la funciones horarias";
		name = "ContextTime";
		type = ContextType.Tipo1;
		permissions = new HashSet<Permission>();
		frecuency = 10f;
		sensors = new HashSet<Sensor>();
		// functions.add(new FunctionTimer2<String>());

	}

	public Function<?> getFunctionByName(String functionName) {
		Function<?> function = null;
		for (Function<?> f : getAllFunctions()) {
			if (f.getFunctionName().equals(functionName)) {
				function = f;
				break;
			}
		}
		return function;
	}

	public Set<Function<?>> getAllFunctions() {
		return functions;
	}

	public String getDescription() {
		return description;
	}

	public String getName() {
		return name;
	}

	public ContextType getContextType() {
		return type;
	}

	public Set<Permission> getPermissions() {
		return permissions;
	}

	public Set<Sensor> getAllSensors() {
		return sensors;
	}

	public Float getFrecuency() {
		return frecuency;
	}

	public Map<String, Object> onChange() {
		Map<String, Object> map = new HashMap<String, Object>();
		Date d = Calendar.getInstance().getTime();
		map.put("Hora", d);
		return map;
	}

	public Boolean isAlive() {
		return true;
	}

	public void correctInstalation() {
		// TODO Auto-generated method stub

	}

	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return myStub;
	}

	public Boolean sendData(Map<String, Object> info) {
		Boolean res = false;
		return res;
	}

	public void onCreate() {
		super.onCreate();
		ServiceConnection conn = new ServiceConnection() {
			public void onServiceConnected(ComponentName name, IBinder service) {
				myRemoteService = IMiRemoto.Stub.asInterface(service);
			}

			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
			}
		};
		Intent i = new Intent();
		i.setClassName("com.example.servicioservidor",
				"com.example.servicioservidor.Servicio");
		bindService(i, conn, Context.BIND_AUTO_CREATE);
	}

	public void onDestroy() {
		super.onDestroy();
	}

	IMiRemoto.Stub myStub = new IMiRemoto.Stub() {
		@Override
		public String getResultado() throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String setRetonar() throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean sendMensaje(Map p1) throws RemoteException {
			boolean t=myRemoteService.sendMensaje(p1);
			return t;
		}

	};

}
